<template>
	<view>
		<u-gap></u-gap>
		<view class="box">
			<view class="product">
				<u-image :src="cover" width="144rpx" height="144rpx" border-radius="10rpx"></u-image>
				<view class="info">
					<view class="name">{{storeName}}</view>
					<u-rate inactive-icon="star-fill" inactive-color="#E1E0E0" active-color="#EABC05" :count="count" v-model="value"></u-rate>
				</view>
			</view>
			<u-field :field-style="fieldStyle" type="textarea" :border-bottom="false" label-width="0" v-model="remark" label=" "
			 placeholder="感觉这件商品怎么样？付费都还满意吗？来叨叨几句吧~">
			</u-field>
			<view class="upload">
				<view class="image" v-for="(item,index) in imgs" :key="index">
					<u-image width="148rpx" height="148rpx" border-radius="10rpx" :src="item"></u-image>
					<view class="remove">
						<u-icon @click="imgs.splice(index,1)" name="trash" color="#ffffff"></u-icon>
					</view>
				</view>
				<view class="add" @click="addImg">
					<u-icon name="plus" color="#AAAAAA" size="40rpx"></u-icon>
				</view>
			</view>
		</view>

		<view class="btn">
			<view class="box" @click="submit">
				提交评论
			</view>
			<view class="safe-area-inset-bottom"></view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				count: 5,
				value: 5,
				imgs: [],
				cover: '',
				remark: '',
				fieldStyle: {
					'min-height': '250rpx'
				},
				storeName: ''
			};
		},
		onLoad(options) {
			console.log(options)
			this.$u.get('orderform/info/' + options.orderNumber).then(res => {
				console.log(res);
				this.cover = this.imageURL + res.items[0].goodsPhoto.split(',')[0]
				this.storeName = res.items[0].goodsName
				this.orderData = res
				console.log(this.cover);
			})
		},
		methods: {
			submit() {
				const self = this
				let new_arr = []
				this.imgs.forEach(item => {
					uni.uploadFile({
						url: this.$u.http.config.baseUrl + '/upload/userPhoto',
						filePath: item,
						name: 'file',
						success(res) {
							let data = JSON.parse(res.data)
							new_arr.push(data.data[0].path + '/' + data.data[0].name)
							if (new_arr.length == self.imgs.length) {
								console.log('传完了');
								self.$u.post('orderformcomment/saveGoodsComment', {
									orderItemId: self.orderData.items[0].id,
									totalScore: self.value,
									context: self.remark,
									photoUrls: new_arr.join(',')
								}).then(res => {
									console.log(res);
									uni.showToast({
										title: '评价已提交',
										icon: 'none'
									})
									setTimeout(() => {
										uni.navigateBack()
									}, 1500)
								})
							}
						}
					})
				})
			},
			addImg() {
				const self = this
				if (this.imgs.length >= 3) {
					uni.showToast({
						title: '最多只能选择3张图片，请删除对应图片后再试',
						icon: 'none'
					})
					return
				}
				uni.chooseImage({
					count: 3,
					success(res) {
						console.log(res);
						self.imgs = [...self.imgs, ...res.tempFilePaths]
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.btn {
		position: fixed;
		bottom: 30rpx;
		width: 100%;
		padding: 0 30rpx;
		left: 0;

		.box {
			line-height: 100rpx;
			color: #FFFFFF;
			background: linear-gradient(90deg, #FF3768, #FC6161);
			box-shadow: 0px 10rpx 14rpx 2rpx rgba(255, 77, 70, 0.3);
			border-radius: 20rpx;
			font-size: 30rpx;
			text-align: center;
		}
	}

	.upload {
		padding-bottom: 30rpx;
		flex-wrap: wrap;
		display: flex;
		padding-left: 15rpx;

		.image {
			margin-top: 15rpx;
			margin-left: 15rpx;
			position: relative;

			.remove {
				position: absolute;
				top: 0;
				right: 0;
				padding: 5rpx 10rpx;
				border-top-right-radius: 10rpx;
				background-color: rgba($color: #000000, $alpha: .3);
			}
		}

		.add {
			width: 148rpx;
			height: 148rpx;
			margin-left: 15rpx;
			margin-top: 15rpx;
			border-radius: 10rpx;
			border: 1rpx solid #AAAAAA;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}

	page {
		background-color: #f7f7f7;
		padding: 0 30rpx;
		box-sizing: border-box;
	}

	.box {
		background-color: #FFFFFF;
		border-radius: 20rpx;

		.product {
			display: flex;
			padding: 30rpx;
			padding-bottom: 0;

			.info {
				margin-left: 20rpx;

				.name {
					margin-bottom: 10rpx;
				}
			}
		}
	}
</style>
